package com.study.utils;

import cn.hutool.core.util.RandomUtil;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @author zhuzi
 * 2025/2/7 上午9:04
 */
public class CookieKey {


    /**
     * 从aes.min.js 使用kimi改成Java写法
     * 但是好像 随机数 60位也行？RandomUtil.randomString(60);
     */
    public static class ThsCookie {
        public static void main(String[] args) {
            String s = thsCookie();
            System.out.println(s);
            System.out.println(RandomUtil.randomString(64));
        }

        public static String thsCookie() {
            // 模拟随机数据和哈希过程
            String data = generateRandomData();
            return hashData(data);
        }

        private static String generateRandomData() {
            Random random = new Random();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 32; i++) {
                sb.append((char) (random.nextInt(26) + 'a'));
            }
            return sb.toString();
        }

        private static String hashData(String data) {
            try {
                MessageDigest digest = MessageDigest.getInstance("SHA-256");
                byte[] hashBytes = digest.digest(data.getBytes());
                return bytesToHex(hashBytes);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("SHA-256 algorithm not found", e);
            }
        }

        private static String bytesToHex(byte[] bytes) {
            StringBuilder hexString = new StringBuilder();
            for (byte b : bytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        }
    }
}
